package com.cskaoyan.mall.controller.mall;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.mall.model.goods.*;
import com.cskaoyan.mall.service.GoodsService;
import com.cskaoyan.mall.service.GoodsServiceImpl;
import com.cskaoyan.mall.util.RequestBodyUtils;
import com.cskaoyan.mall.util.Result;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: jyf2000jyf
 */
@WebServlet("/api/mall/goods/*")
public class GoodsServlet extends HttpServlet {
    private GoodsService goodsService = new GoodsServiceImpl();
    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/mall/goods/", "");
        if ("askGoodsMsg".equals(op)) {
            askGoodsMsg(request, response);
        }
    }

    private void askGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = RequestBodyUtils.getRequestBody(request);
        AskGoodsMsgBO askGoodsMsgBO = gson.fromJson(requestBody, AskGoodsMsgBO.class);
        Result result;
        boolean status = goodsService.askGoodsMsg(askGoodsMsgBO);
        if (status) {
            result = new Result(0, "留言成功", null);
            response.getWriter().println(gson.toJson(result));
            return;
        } else {
            result = new Result(10000, "留言失败", null);
            response.getWriter().println(gson.toJson(result));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/mall/goods/", "");
        if ("searchGoods".equals(op)) {
            searchGoods(request, response);
        } else if ("getGoodsInfo".equals(op)) {
            getGoodsInfoWithoutGoodsVO(request, response);
        } else if ("getGoodsMsg".equals(op)) {
            getGoodsMsg(request, response);
        } else if ("getGoodsByType".equals(op)) {
            getGoodsByType(request, response);
        } else if ("getGoodsComment".equals(op)) {
            getGoodsComment(request, response);
        }
    }

    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer typeId = Integer.parseInt(request.getParameter("typeId"));
        List<GoodsByTypeVO> goodsByTypeVOList = goodsService.getGoodsByType(typeId);
        Result result = new Result(0, null, goodsByTypeVOList);
        response.getWriter().println(gson.toJson(result));
    }

    private void searchGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String goodsName = request.getParameter("keyword");
        Result result;
        if (StringUtils.isEmpty(goodsName)) {
            result = new Result(10000, "输入不能为空！", null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        List<GoodsByNameVO> goodsByNameVOList = goodsService.getGoodsByName(goodsName);
        result = new Result(0, null, goodsByNameVOList);
        response.getWriter().println(gson.toJson(result));
    }

    private void getGoodsInfoWithoutGoodsVO(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer goodsId = Integer.parseInt(request.getParameter("id"));
        GoodsInfoWithoutGoodsVO goodsInfo = goodsService.getGoodsInfoWithoutGoodsVO(goodsId);
        Result result = new Result(0, null, goodsInfo);
        response.getWriter().println(gson.toJson(result));
    }

    private void getGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        List<GoodsMsgVO> goodsMsgVOList = goodsService.getGoodsMsg(id);
        Result result = new Result(0, null, goodsMsgVOList);
        response.getWriter().println(gson.toJson(result));
    }

    private void getGoodsComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer goodsId = Integer.parseInt(request.getParameter("goodsId"));
        GoodsCommentVO goodsCommentVO = goodsService.getGoodsComment(goodsId);
        Result result;
        if (goodsCommentVO == null) {
            result = new Result(0, null, new GoodsCommentVOWhileNull());
            response.getWriter().println(gson.toJson(result));
            return;
        }
        result = new Result(0, null, goodsCommentVO);
        response.getWriter().println(gson.toJson(result));
    }
}
